<?php
// 应用公共文件

/**
* 百度时段简化算法
*
* @param tValue
* @return
*/
function getAlgorithm( $array, $weekDay ) {
	$number = count($array);
	if ( $number ==0 ){
		return false;
	}
	$i = 0;
	$n = 0;
	$result = [];
	$length = $number - 1;
	foreach ($array as $key => $value) {
		if($key < $length) {
		if(($array[$i]+1) == $array[$i+1]) {
			$result[$n][] = $array[$i];
		} else {
			$result[$n][] = $array[$i];
			$n++;
		}
		$i++;
		} else {
			if(($array[$i]-1)== $array[$i-1]) {
				$result[$n][] = $array[$i];
			}else{
				$result[$n+1][] = $array[$i];
			}
		}
	}
	$r = [];
	foreach ($result as $value) {
		array_push($r, [
			'weekDay' => $weekDay,
			'startHour' => $value[0],
			'endHour' => end($value) + 1
		]);
	}
	return $r;
}

/**
* 时间戳格式化
*
* @param tValue
* @return
*/
function cloud_datetime_format( $createTimeStart, $createTimeEnd, $format = true ) {
	$time1 = new \DateTime($createTimeStart);
	$time2 = new \DateTime($createTimeEnd);
	if( $format ) {
		return [ $time1->format('U'), $time2->format('U')];
	}
	return [ $time1->format('Y-m-d H:i:s'), $time2->format('Y-m-d H:i:s')];
}

/**
* 域名格式化
*
* @param tValue
* @return
*/
function cloud_domain_format($DomainUrl) {
	if(empty($DomainUrl)){
		return false;
	}
	$returnUrl = '';
	$parseArray = parse_url($DomainUrl);
	if(isset($parseArray['host']) ){
		$returnUrl .= $parseArray['host'];
	}
	if(isset($parseArray['path']) ) {
		$returnUrl .= $parseArray['path'];
	}
	if(empty($returnUrl)){
		return false;
	}
	return rtrim($returnUrl, '/');
}

function cloud_get_array_value( $input, $needle, $column_key, $result, $default = 'null' ) {
	$index = array_search($needle, array_column($input, $column_key));
	if( $index === false ) {
		return $default;
	}
	return $input[$index][$result];
}

function cloud_get_array( $input, $needle, $column_key) {
	$index = array_search($needle, array_column($input, $column_key));
	if( $index === false ) {
		return false;
	}
	return $input[$index];
}

function doGetRequest($url) {
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl, CURLOPT_HEADER, false);
	curl_setopt($curl, CURLOPT_TIMEOUT, 1000);
	curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 1000);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
	$result = curl_exec($curl);
	curl_close($curl);
	return $result;
}

function doPostRequest($url, $data) {
	$ch = curl_init ();
	curl_setopt ( $ch, CURLOPT_URL, $url );
	curl_setopt ( $ch, CURLOPT_POST, true );
	curl_setopt ( $ch, CURLOPT_HEADER, false);
	curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
	curl_setopt ( $ch, CURLOPT_TIMEOUT, 30);
	curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT , 30);
	curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
	curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
	curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, false );
	curl_setopt ( $ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json; charset=utf-8'] );
	$result = curl_exec ( $ch );
	curl_close ($ch );
	return $result;
}

